<?php
namespace lib\YanPHP;

class Route
{
    /**
     * 开始运行路由
     */
    public function start(){
        $model = config('Route_model');
        switch ($model) {
            case '1':
                self::model_1();
                break;

            case '2':
                self::model_2();
                break;

            default:
                throw new \Exception("错误的路由模式 $model");
                break;
        }
    }

    /**
     * 模式一 普通模式 index.php?m=m&c=c&a=a
     */
    public function model_1(){
        $m = !empty($_GET['m']) ? ucfirst($_GET['m']) : config('default_m');
        $c = !empty($_GET['c']) ? $_GET['c'] : config('default_c');
        $a = !empty($_GET['a']) ? $_GET['a'] : config('default_a');

        $GLOBALS['url_info'][]=$m;
        $GLOBALS['url_info'][]=$c;
        $GLOBALS['url_info'][]=$a;

        if ( !file_exists(ROOT_PATH . __APP__ . '/controll/' . $m)) {
            throw new \Exception("模块 $m 不存在！");
        }else{
            if ( !file_exists(ROOT_PATH .  __APP__ . "/controll/$m/$c.php")) {
                throw new \Exception("控制器 $c 不存在！");
            }else{
                $class_name =  '/controll\\'.$m.'\\'.$c;
                $class = new $class_name;
                if ( $a == '' || !method_exists($class, $a)) {
                    throw new \Exception("控制器 $class_name 中不存在 $a 方法！");
                }
            }
        }

        $class->$a();
    }

    /**
     * 模式二 优化模式 index.php/m/c/a.html
     */
    public function model_2(){
        $path_info  = self::Path_Info();

        if (empty($path_info)) {
            // $m = config('default_m');
            // $c = config('default_c');
            // $a = config('default_a');
            self::model_1();die;
        }else{
            // $path_info  = trim($path_info, config('url_suffix'));     // 删除后缀名
            $path_info  = str_replace(config('url_suffix'), '', $path_info);     // 删除后缀名
            $path_info  = substr($path_info, 1);
            $path       = explode('/', $path_info);
            $m = ucfirst( array_shift($path) ); // 模块
            $m = !empty($m) ? $m : config('default_m');
            $c = array_shift($path); // 控制器
            $c = !empty($c) ? $c : config('default_c');
            $a = array_shift($path); // 方法
            $a = !empty($a) ? $a : config('default_a');
            # 如果URL还有后续参数
			if ( count($path) !=0 ) {
                # 将参数转化为GET参数
				foreach ($path as $key => $value) {
					if( $key%2 == 0 ){
						$_GET[$value] = '';
					}else{
						$_GET[$path[$key-1]] = $value;
					}
				}
			}
        }
        # 将模块、控制器、方法存入全局变量
        $GLOBALS['url_info'][]=$m;
        $GLOBALS['url_info'][]=$c;
        $GLOBALS['url_info'][]=$a;
        
        if ( !file_exists(ROOT_PATH . __APP__ . '/controll/' . $m)) {
            throw new \Exception("模块 $m 不存在！");
        }else{
            if ( !file_exists(ROOT_PATH . __APP__ . "/controll/$m/$c.php")) {
                throw new \Exception("控制器 $c 不存在！");
            }else{
                $class_name = 'controll\\'.$m.'\\'.$c;
                $class = new $class_name;
                if ( $a == '' || !method_exists($class, $a)) {
                    throw new \Exception("控制器 $class_name 中不存在 $a 方法！");
                }
            }
        }
        
        $class->$a();
    }

    /**
     * 获得URL
     * @return string
     */
    static public function Path_Info() {
		# nginx环境下需要修改.conf文件才能支持
        if (isset($_SERVER['PATH_INFO'])) {
			return $_SERVER['PATH_INFO'];
		}
		
		if (isset($_SERVER['REDIRECT_PATH_INFO'])) {
			return $_SERVER['REDIRECT_PATH_INFO'];
		}
		
		if (isset($_SERVER['REDIRECT_URL'])) {
			return $_SERVER['REDIRECT_URL'];
		}
		
		return false;
    }
}
